//
//  NSKeyedArchiverSaveData.m
//  MISCloud
//
//  Created by goldeneye on 13-8-11.
//  Copyright (c) 2013年 goldeneye. All rights reserved.
//

#import "NSKeyedArchiverUserLogin.h"
#import "FilePathHelper.h"

#define kUserLoginInfoKey @"UserLoginArchiveKey"
#define kUserLoginInfoFileName @"UserLoginInfo"

@implementation NSKeyedArchiverUserLogin

#pragma mark -- 用户配置信息
#pragma mark UserLoginInfo

/******存储用户登录信息*********/
+ (BOOL) saveUserLoginData:(UserLoginNSCoding*)userInfo
{
    [self delectUserLoginData];
    
    NSString *Path = [FilePathHelper getUserInfoPath];
    NSString *filePath = [Path stringByAppendingPathComponent:kUserLoginInfoFileName];
    
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *vdArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [vdArchiver encodeObject:userInfo forKey:kUserLoginInfoKey];
    [vdArchiver finishEncoding];
    
    BOOL isSaveSuccess = [data writeToFile:filePath atomically:YES];
    //NSLog(@"用户信息保存：%@",isSaveSuccess ? @"成功" : @"失败");
    return isSaveSuccess;
}

/******删除用户登录信息*********/
+ (void) delectUserLoginData
{
    NSString *Path = [FilePathHelper getUserInfoPath];
    NSString *filename = [Path stringByAppendingPathComponent:kUserLoginInfoFileName ];
   
    NSFileManager *defaultManager = [NSFileManager defaultManager];
    if ([defaultManager isDeletableFileAtPath:filename]) {
        [defaultManager removeItemAtPath:filename error:nil];
    }
}

/******获取用户登录信息*********/
+ (UserLoginNSCoding*) getUserLoginData
{
    NSString *Path = [FilePathHelper getUserInfoPath];
    NSString *filePath = [Path stringByAppendingPathComponent:kUserLoginInfoFileName];
    
    NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
    NSKeyedUnarchiver *vdUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    UserLoginNSCoding *array  = [vdUnarchiver decodeObjectForKey:kUserLoginInfoKey];
    [vdUnarchiver finishDecoding];

    return array;
}

@end
